// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: GPL-3.0-only

package storage

import (
	"os"
	"path/filepath"

	"gitcode.com/openseaotter/so_server/storage/badger_dao_impl"
	"gitcode.com/openseaotter/so_server/storage/core"
	"gitcode.com/openseaotter/so_server/storage/local_store_impl"
	"gitcode.com/openseaotter/so_server/utils"
)

var AuthSecretDao core.AuthSecretDao
var ImageGroupDao core.ImageGroupDao
var ImageInfoDao core.ImageInfoDao
var ImageReferenceDao core.ImageReferenceDao
var MemberDao core.MemberDao
var WatchInfoDao core.WatchInfoDao
var ChangeDao core.ChangeDao
var BlobStore core.BlobStore
var ChangeNoticeDao *_ChangeNoticeDao

func InitBadgerDb() (err error) {
	AuthSecretDao, err = badger_dao_impl.NewAuthSecretDao()
	if err != nil {
		return
	}
	ImageGroupDao, err = badger_dao_impl.NewImageGroupDao()
	if err != nil {
		return
	}
	ImageInfoDao, err = badger_dao_impl.NewImageInfoDao()
	if err != nil {
		return
	}
	ImageReferenceDao, err = badger_dao_impl.NewImageReferenceDao()
	if err != nil {
		return
	}
	MemberDao, err = badger_dao_impl.NewMemberDao()
	if err != nil {
		return
	}
	WatchInfoDao, err = badger_dao_impl.NewWatchInfoDao()
	if err != nil {
		return
	}
	ChangeDao, err = badger_dao_impl.NewChangeDao()
	if err != nil {
		return
	}
	return
}

func InitLocalStore() {
	BlobStore = local_store_impl.NewLocalStoreImpl()
}

func InitUploadStore() {
	dirPath := filepath.Join(utils.DATA_DIR_PATH, "uploads")
	os.RemoveAll(dirPath)      //skip error check
	os.MkdirAll(dirPath, 0700) //skip error check
}

func InitChangeNoticeDao() {
	ChangeNoticeDao = newChangeNoticeDao()
}

func CalcUploadFile(uuid string) string {
	return filepath.Join(utils.DATA_DIR_PATH, "uploads", uuid)
}
